/*
 * Created on 2022-07-29 13:45
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.yzbdl.lanius.ui.spoon.delegates;

import org.pentaho.di.ui.core.PropsUI;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.di.ui.spoon.TabMapEntry;
import org.pentaho.di.ui.spoon.delegates.SpoonDelegate;
import org.pentaho.xul.swt.tab.TabItem;
import org.pentaho.xul.swt.tab.TabSet;
import org.yzbdl.lanius.ui.plugin.dialog.PluginListDelegate;

/**
 * 插件管理
 *
 * @author tanpeng@yzbdl.ac.cn
 * @date 2022-07-29 13:45
 */
public class SpoonPluginManageDelegate extends SpoonDelegate {
    /** for i18n purposes, needed by Translator2!! */
    private static Class<?> PKG = Spoon.class;

    public SpoonPluginManageDelegate(Spoon spoon) {
        super(spoon);
    }

    public boolean addSpoonPluginManage(String name) {
        TabSet tabfolder = spoon.tabfolder;

        try {
            TabMapEntry tabMapEntry = spoon.delegates.tabs.findTabMapEntry(name, TabMapEntry.ObjectType.PLUGIN);
            if (tabMapEntry == null) {
                final PluginListDelegate pluginListDelegate = new PluginListDelegate(tabfolder.getSwtTabset());
                PropsUI props = PropsUI.getInstance();
                TabItem tabItem = new TabItem(tabfolder, name, name, props.getSashWeights());
                tabItem.setImage(GUIResource.getInstance().getImageLogoSmall());
                tabItem.setControl(pluginListDelegate);

                tabMapEntry =
                    new TabMapEntry(tabItem, name, name, null, null, pluginListDelegate, TabMapEntry.ObjectType.PLUGIN);
                spoon.delegates.tabs.addTab(tabMapEntry);

            }
            int idx = tabfolder.indexOf(tabMapEntry.getTabItem());

            // keep the focus on the graph
            tabfolder.setSelected(idx);
            return true;
        } catch (Throwable e) {
            boolean ok = false;
            if (!ok) {
                // Log an error
                //
                log.logError("无法打开插件管理列表", e);
                return false;
            } else {
                return true;
            }
        }
    }

}
